package com.yiibai.springmvc.configuration;

import java.io.IOException;
import java.util.Locale;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import com.yiibai.springmvc.view_resoulver.CustomFreeMarkerViewResolver;
import com.yiibai.springmvc.view_resoulver.HtmlResourceView;

import freemarker.template.TemplateException;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.yiibai.springmvc")
public class DispatcherConfig extends WebMvcConfigurerAdapter{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean(name="htmlViewResolver")
    public ViewResolver htmlViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();  
        viewResolver.setViewClass(HtmlResourceView.class); 
        viewResolver.setCache(false);
        viewResolver.setPrefix("/WEB-INF/views/");  
        viewResolver.setSuffix(".html");  
        viewResolver.setOrder(0);  
        return viewResolver;  
    }

    /*
    @Bean(name="freeMarkerViewResolver")
    public CustomFreeMarkerViewResolver freeMarkerViewResolver() {
        CustomFreeMarkerViewResolver resolver = new CustomFreeMarkerViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".ftl");
        resolver.setCache(false);
        
        resolver.setContentType("text/html;charset=UTF-8");
        resolver.setRequestContextAttribute("rc");
        resolver.setExposeSpringMacroHelpers(true);
        resolver.setExposeRequestAttributes(true);
        resolver.setExposePathVariables(true);
        resolver.setExposeSessionAttributes(true);
        resolver.setExposeContextBeansAsAttributes(true);
        
        resolver.setOrder(1);
        
        return resolver;
    }
    */
    
    @Bean(name="jspViewResolver")
    public ViewResolver jspViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setCache(false);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setOrder(2);
        return viewResolver;
    }
    
    @Bean(name="freemarkerViewResolver2")
    public ViewResolver freemarkerViewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setCache(false);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".ftl");
        resolver.setOrder(100);
        return resolver;
    }
/*
    @Bean(name="freeMarkerConfig")
    public FreeMarkerConfigurer freeMarkerConfigurer(WebApplicationContext applicationContext) throws IOException, TemplateException {
        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();

        freeMarkerConfigurer.setServletContext(applicationContext.getServletContext());
        freemarker.template.Configuration configuration = freeMarkerConfigurer.getConfiguration();
        configuration.addAutoInclude("/templates/include-common.ftl");
        configuration.setNumberFormat("0.######");
        configuration.setWhitespaceStripping(true);


        configuration.setDefaultEncoding("UTF-8");
        configuration.setOutputEncoding("UTF-8");
        configuration.setURLEscapingCharset("UTF-8");
        configuration.setServletContextForTemplateLoading(applicationContext.getServletContext(), "/WEB-INF/ftl/");
        configuration.setTemplateUpdateDelay(0);


        configuration.setLocale(Locale.ENGLISH);
        configuration.setTagSyntax(1); //ensure tags are angle brackets
        configuration.setBooleanFormat("TRUE,FALSE");
        configuration.setWhitespaceStripping(true);
        configuration.setStrictBeanModels(false);

        freeMarkerConfigurer.setConfiguration(configuration);

        return freeMarkerConfigurer;
    }
    */
}